package com.tuzhi.test.sys.jpa.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tuzhi.base.jpa.repository.BaseEnity;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Data
@Entity
@Table(name = "sys_role", schema = "tuzhi", catalog = "")
public class SysRoleEntity extends BaseEnity {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "id", nullable = false)
    private Long id;
    @Basic
    @Column(name = "title", nullable = true, length = 32)
    private String title;
    @Basic
    @Column(name = "name", nullable = true, length = 32)
    private String name;
    @Basic
    @Column(name = "remark", nullable = true, length = 128)
    private String remark;
    @Basic
    @CreatedDate
    @Column(name = "create_date", updatable = false)
    private Date createDate;
    @Basic
    @LastModifiedDate
    @Column(name = "update_date", nullable = true)
    private Date updateDate;
    @Basic
    @Column(name = "create_by", nullable = true)
    private Long createBy;
    @Basic
    @Column(name = "update_by", nullable = true)
    private Long updateBy;
    @Basic
    @Column(name = "status", nullable = true)
    private Integer status = 0;

    @JsonIgnore
    @ManyToMany(mappedBy = "roles")
//    @JoinTable(name = "sys_user_role",
//            // joinColumns,当前对象在中间表中的外键,
//            // name="当前表的外键的名字（可随便取）“
//            // referencedColumnName=“参照该表的主键”
//            joinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")},
//            //inverseJoinColumns :对方对象在中间表的外键
//            inverseJoinColumns = {@JoinColumn(name = "user_id",referencedColumnName = "id")}
//    )
    private Set<SysUserEntity> users = new HashSet<>();

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SysRoleEntity that = (SysRoleEntity) o;
        return Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
